﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.User;
public partial class sysadmin_UserManager_Department:PowerCreator.LMS.Web.Base.BasePage
{
    Department opDepart = new Department();
    User opUser = new User();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();

            BinderRpt();
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_Department_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        base.InitPage();
    }

    //绑定Rpt
    private void BinderRpt()
    {
        string search = "";
        DataTable dt = opDepart.Query(search);
        ArrayList list = new ArrayList();
        list = opDepart.GetDepartmentTree(dt);
        if (list.Count == 0)
        {
            ListItem li = new ListItem();
            li.Text = GetLanageValueByKey("department_defaultdeparmentname", PowerCreator.LMS.Language.LanagetItemType.Page);
            li.Value = "0";
            list.Add(li);
        }

        rptDepartment.DataSource = list;
        rptDepartment.DataBind();
        for (int i = 0; i < rptDepartment.Items.Count; i++)
        {
            Button btnadd = (Button)rptDepartment.Items[i].FindControl("btn_Add");
            btnadd.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button btn_Editor = (Button)rptDepartment.Items[i].FindControl("btn_Editor");
            btn_Editor.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o1", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button Button1 = (Button)rptDepartment.Items[i].FindControl("Button1");
            Button1.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o2", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
    }

    protected void rptDepartment_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "btnadd")
        {
            string url = "AddDepartment.aspx?DepartmentID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btnedit")
        {
            string url = "EditDepartment.aspx?DepartmentID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btndel")
        {
            int count = opDepart.CountParent(int.Parse(e.CommandArgument.ToString()));
            if (count > 1)
            {
                //Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
                lbl_msg_description.Text = GetLanageValueByKey("department_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                //lbl_msg_description.Text = "部门下有子部门，不允许删除该部门!";
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return;
            }
            string search = "";
            VOTDepartment voDepartment = opDepart.Load(int.Parse(e.CommandArgument.ToString()));
            if (voDepartment.DepartmentType == 1)
            {
                search = string.Format("FKTeacherDepartmentId = {0}", e.CommandArgument);
                if (opUser.QueryTeacherCount(search) != 0)
                {
                    lbl_msg_description.Text = GetLanageValueByKey("department_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
                    //lbl_msg_description.Text = "该部门下有有用户，不允许删除该部门!";
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                    return;
                }
            }
            else if (voDepartment.DepartmentType == 2)
            {
                search = string.Format("FKStudentDepartmentId = {0}", e.CommandArgument);
                if (opUser.QueryStudentCount(search) != 0)
                {
                    lbl_msg_description.Text = GetLanageValueByKey("department_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                    //lbl_msg_description.Text = "该班级下有有用户，不允许删除该班级!";
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                    return;
                }
            }
            
            if (opDepart.Del(int.Parse(e.CommandArgument.ToString())))
            {
                BinderRpt();
            }
        }
    }
}
